Repository navigation
Key the other-method query builder cache by the bound declaring class - #821
Open
giosh94mhz wants to merge 1 commit into
Open
giosh94mhz wants to merge 1 commit into
giosh94mhz wants to merge 1 commit into
Conversation
OtherMethodQueryBuilderParser cached the query builders of an inherited method by its declaring class name, while the builders depend on the template types the caller binds. The first repository analysed decided the entity for every sibling, so a generic base repository made the DQL rule report fields of the wrong entity, varying with analysis order.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Repositories extending a generic base repository (
@template T of object) can get the wrong entity forTin their query builders: when two subclasses bindTtoEntityAandEntityB, both end up analysed asEntityA, because the query builders of the inherited method are cached once per declaring class.QueryBuilderDqlRulethen reports false DQL errors, depending on the analysis order.Details
OtherMethodQueryBuilderParsercaches the query builders returned by a method underfile-DeclaringClass-method. When that method is inherited from a generic base repository, the declaring class reflection is already bound to the caller's template types, so the builders it returns depend on that binding, but the cache key ignores it. The first repository analysed decides the entity for all its siblings.The new fixture
tests/Rules/Doctrine/ORM/data/query-builder-dql-generic-repository.phpreproduces it:AbstractGenericRepository<T of GenericRepositorySuperclass>has a protectedcreateSharedQueryBuilder(),GenericRepositoryA/GenericRepositoryBbindTto two entities with different fieldse.onlyAande.onlyBon it.Without the fix,
QueryBuilderDqlRuleTest::testQueryBuilderInheritedFromGenericRepository()fails with:The error depends on the analysis order, so it comes and goes between runs and between machines. When the abstract class calls the method itself first, the builder is cached with
Tresolved to its bound, and every subclass getsFROMthe bound class.Fix
The cache key now also includes the active template type map of a generic declaring class, described with
VerbosityLevel::cache(). Non-generic classes keep the same key as before.Testing
I've done testing locally so far. The
make lint,make csandmake phpstanare clean. The test suite passes on PHP 7.4 and 8.5 with both lowest and highest dependencies, and on ORM 3 / DBAL 4 with the compatibility patches.For every other test I'll wait your CI results